<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;

class User extends AdminController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //查询role,user 
        $data = db('admin_user')->select();
        $list = Db::field('ur.uid,r.name')
        ->table(['s75_admin_user'=>'u','s75_role'=>'r','s75_user_role'=>'ur'])
        ->where('u.id=uid')->where('r.id=rid')->where('status','=','1')
        ->select();
        
        // 遍历数组
        foreach ($data as $k => $v) {
            $data[$k]['rname'] = '';
            foreach ($list as $k1 => $vo) {
                if ($vo['uid']==$v['id']){
                 $data[$k]['rname'] .= $vo['name'].' ';
                }
            }   
        }

        // 加载页面
        return view('user/list',[
            'title'=>'用户列表',
            'list'=>$data,
        ]);


    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {   
        
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
         // 新增数据
        $data = $request->post();
        $data =array_diff_key($data,['repass'=>'repass']);
        $data['userpass'] = md5($data['userpass']);
        $row = db('admin_user')->insert($data);
        
        // 显示添加
        if ($row === null) {
            $info['status'] = false;
            $info['title'] = '查无数据';
            $info['datas'] = '查无数据,请重试';
        } else {
            $info['status'] = true;
            $info['title'] = $row['id'];
            $info['datas'] = '用户修改成功!';
        }

        return json($info);
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {   
       // 显示查询
        $name = db('admin_user')->field('username')->find($id);
        $list = db('role')->field('name,id')->where('status','eq','1')->select();
        $data = Db::field('rid')
        ->table(['s75_admin_user'=>'u','s75_role'=>'r','s75_user_role'=>'ur'])
        ->where('u.id=uid')->where('r.id=rid')->where('u.id','=',$id)
        ->select();
       
         $arr = [];     // 角色池
        foreach ($data as $k => $v) { $arr[] = $v['rid'];}
        // 当前id存在list中给true
        foreach ($list as $k => $v) {
            $list[$k]['mark']= 0;
            if (in_array($v['id'],$arr)) {
                $list[$k]['mark'] = 1;
            }
        }
 
        return view('user/read',[
            'title'=>$name['username'].'的角色信息',
            'list'=>$list,
            'id'=>$id,
            'arr'=>$arr
        ]);
        
    }

     /**
     * 执行分配角色
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function allot(Request $request)
    {   
         $data = $request->put();
        if (empty($data)) { $this->redirect('admin/User/read'); }
        
       // 拼键值对
        $count = count($data['rid']);
        $arr = [];
        for ($i=0; $i < $count; $i++) { 
          $arr[$i] = ['uid'=>$data['uid'],'rid'=>$data['rid'][$i]];
        }
        // 删除
        $result = db('user_role')->where('uid','=',$data['uid'][0])->delete();
        // 插入
        $result = db('user_role')->insertAll($arr);
        if ($result) {
            return  $this->success('分配完成',url('admin/user/index'));
        }else{
            return $this->error('分配失败');
        }
    }


    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $list = db('admin_user')->where('id',$id)->select();
        return view('user/edit',[
            'title'=>'编辑用户',
            'list'=>$list[0]
        ]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {   
        $data = $request->put();
        $data =array_diff_key($data, ['_method' => "Put"]);
        $result = db('admin_admin_user')->update($data);
        
        // 显示修改
         if ($result > 0) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为' .$id. '的用户修改成功!';
        }else{
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = '修改失败';
        }
        return json($info);
        
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {   
        $result = db('admin_user')->delete($id);

        if ($result) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为'.$id.'的用户删除成功!';
        }else{
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = '删除失败';
        }

        // echo json_encode($result);
        return json($info);
    }
}
